//
//  AdvertViewController.m
//  Wujin
//
//  Created by cz on 2016/12/9.
//  Copyright © 2016年 dong. All rights reserved.
//

#import "AdvertViewController.h"

@interface AdvertViewController ()<UIWebViewDelegate>

@property (nonatomic,strong) UIWebView *webView;

@end

@implementation AdvertViewController

- (UIWebView *)webView {
    if (!_webView) {
        _webView = [[UIWebView alloc] init];
        _webView.backgroundColor = [UIColor whiteColor];
        
        _webView.delegate = self;
    }
    return _webView;
}

- (void)loadView {
    self.view = self.webView;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //给个返回按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backClick)];
    
    [self loadAdvertView];
}

- (void)loadAdvertView {
    //显示网页数据
    NSURL *url = [NSURL URLWithString:[NSString stringWithString:self.urlStr]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //加载一个请求
    [self.webView loadRequest:request];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    NSHTTPURLResponse *response = nil;
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
    
    if (response.statusCode != 200) {
        
        [MRHud showWithFailure:@"您的网络不给力"];
        [self backClick];
        return NO;
    }
    return YES;
}


- (void)backClick{
    //点击返回隐藏模态窗口
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
